<?php
// ontologyRequest.php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-type, Content-length');
header('Content-Type: application/json');

/*
 Put all ontologies in database for easy modifying/extending list.
 Or maybe just text file.
 Fields in database: name, rdfFile, namespace
*/

$ontologyList = array();

mysql_connect("mysql2.nozonenet.com", "duncan_admin", "testing");
mysql_select_db("duncan_radify");
$ontologies = mysql_query("SELECT name, nsName, rdfFile, namespace, description FROM ontology;");
if (mysql_num_rows($ontologies)){
    while ($ontology = mysql_fetch_assoc($ontologies)) {
        extract($ontology, EXTR_PREFIX_ALL, "ontology");
        $thisOntology = array();
        $properties = parseOntologies($ontology_rdfFile, $ontology_namespace);
        $thisOntology = array(
            "name" => $ontology_name,
            "nsName" => $ontology_nsName,
            "namespace" => $ontology_namespace,
            "description" => $ontology_description,
            "properties" => $properties,
        );
        array_push($ontologyList, $thisOntology);
    }
}

/**
 * Parses RDF/XML file and returns an array of all label names
*/
function parseOntologies($file, $ns) {
    $rdfDoc = new DOMDocument();
    $rdfDoc->load($file);
    $xpath = new DOMXPath($rdfDoc);
    $properties = $xpath->query("//@rdf:about");
    $xmlBase = $xpath->query("//@xml:base");
    if ($xmlBase->item(0)->nodeValue == null) {
        // change this to ontology namespace from db...
        $base = $ns;
    }
    else {
        $base = $xmlBase->item(0)->nodeValue;
    }
    $data = array();
    foreach ($properties as $property) {
        $stop = false;
        $isClass = false;
        if (strstr($property->nodeValue,'http')) {
            if (!strstr($property->nodeValue, $base) or $property->nodeValue == $base) {
                // Case where property is different ontology (or just ns decleration). Do nothing.
                $stop = true;
            }
        }
        if ($property->nodeValue == "") {
            $stop = true;
        }
        // if Class (maybe add to a class attribute in array? Because might be useful)
        if (strstr($property->parentNode->nodeName,'Class')) {
            $isClass = true;
        }
        if (!$stop) {
            $label = $property->parentNode->attributes->getNamedItem("label")->nodeValue;
            $comment = $property->parentNode->attributes->getNamedItem("comment")->nodeValue;
            if ($label == null) {
                for ($i = 0; $i < $property->parentNode->childNodes->length; $i++) {
                    if ($property->parentNode->childNodes->item($i)->nodeName == "rdfs:label") {
                        // if has lang - make sure is english
                        if ($property->parentNode->childNodes->item($i)->getAttribute("xml:lang") != '') {
                            if ($property->parentNode->childNodes->item($i)->getAttribute("xml:lang") == "en") {
                                $label = $property->parentNode->childNodes->item($i)->nodeValue;
                            }
                        }
                        else {
                            $label = $property->parentNode->childNodes->item($i)->nodeValue;
                        }
                    }
                }
            }
            if ($comment == null) {
                for ($i = 0; $i < $property->parentNode->childNodes->length; $i++) {
                    if ($property->parentNode->childNodes->item($i)->nodeName == "rdfs:comment") {
                        if ($property->parentNode->childNodes->item($i)->getAttribute("xml:lang") != '') {
                            if ($property->parentNode->childNodes->item($i)->getAttribute("xml:lang") === "en") {
                                $comment = $property->parentNode->childNodes->item($i)->nodeValue;
                            }
                        }
                        else {
                            $comment = $property->parentNode->childNodes->item($i)->nodeValue;
                        }
                    }
                }
            }
            $about = $property->nodeValue;
            // change from URI to CURIE if necessary.
            if(strpos($about, $ns) === 0) {
                $about = substr($about, strlen($ns));
            }
            $thisProperty = array(
                "CURIE" => $about,
                "label" => $label,
                "isClass" => $isClass,
                "comment" => $comment
            );
            array_push($data, $thisProperty);
        }
    }
    return $data;
}

// return JSON data
$jsonData = json_encode($ontologyList);
echo($jsonData);

?>